import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/course_dao.dart';
import 'package:hktk_app/dao/home_dao.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/ienum/home/column_position_enum.dart';
import 'package:hktk_app/ienum/home/column_type_enum.dart';
import 'package:hktk_app/model/course/course_chapter_model.dart';
import 'package:hktk_app/model/data_model.dart';
import 'package:hktk_app/model/home/column_model.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/model/subject/chapter_model.dart';
import 'package:hktk_app/pages/course/free_video.dart';
import 'package:hktk_app/pages/tab/study.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/home/recommend_expandable_widget.dart';
import 'package:hktk_app/widget/subject/chapter_expandable_widget.dart';
import 'package:hktk_app/widget/subject/chapter_leaf_widget.dart';

// 根据考试，列举科目与视频栏目，页面
class VideoListPage extends StatefulWidget {
  @override
  State createState() => _VideoListPageState();
}

class _VideoListPageState extends State<VideoListPage>
    with TickerProviderStateMixin {
  // late TabController _tabController;
  late bool _isLoading = true;

  late int _selectedCategoryIndex = 0;

  // late int _selectedColumnIndex = 0;
  late List<CategoryModel>? _categoryList = [];

  // late List<ColumnModel> _columnList = [];
  // late List chapterList = [];
  final List tabIcons = [
    "images/home/home_tab_13.svg",
    "images/home/home_tab_3.svg",
    "images/home/home_tab_14.svg"
  ];
  late dynamic data;

  @override
  void initState() {
    //   initState
    // IndustryDao.leafList
    super.initState();
    //   初始化获取栏目，再获取第一个栏目对应的视频
    //   左上角下拉切换科目
    //   顶部显示栏目名称
    //   头部点击icon切换栏目
    //   内容区域展示视频
    getInitData();
  }

  Future getInitData() async {
    _categoryList =
        await IndustryDao.leafList(examId: IndustryDao.getExamKey().examId);
    // 获取ship
    data = await HomeDao.getVideoTrial(categoryId: _categoryList?[0].id!);

    _isLoading = false;
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void dispose() {
    //   dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        // title: Text(_columnList.isNotEmpty ? _columnList[_selectedColumnIndex].name! : ""),
        // title: Text(_columnList.isNotEmpty ? _columnList[_selectedColumnIndex].name! : ""),
        centerTitle: true,
      ),
      body: SafeArea(
          child: _isLoading
              ? Center(child: CircularProgressIndicator())
              : Column(
                  children: [
                    // 科目切换组件
                    Row(
                      children: [
                        Container(
                          height: 60,
                          margin: EdgeInsets.only(left: 16),
                          child: DropdownButton(
                              value: _selectedCategoryIndex,
                              dropdownColor: Color(0xffE3EDFF),
                              items: [
                                for (var i = 0; i < _categoryList!.length; i++)
                                  DropdownMenuItem<int>(
                                    value: i,
                                    child: Text(_categoryList![i].name!),
                                  )
                              ],
                              onChanged: (v) {
                                _selectedCategoryIndex = v!;

                                //   切换科目，重新获取栏目，及章节,重置选择的栏目
                                // HomeDao.columnByCategory(
                                //   categoryId: _categoryList![v].id!,
                                //   groupId: 6,
                                //   columnEnum: ColumnPositionEnum.jgqy,
                                // ).then((res) {
                                //   _columnList = res.where((e) {
                                //     return e.type == ColumnTypeEnum.video;
                                //   }).toList();
                                //   _selectedColumnIndex = 0;
                                //
                                //   chapterList = [];
                                //   if (_columnList.isNotEmpty) {
                                //     DataModel dataModel =
                                //         dataUtilComm.dataModel;
                                //     CourseDao.listByColumn(
                                //             industryCode: dataModel.industryId,
                                //             examId: dataModel.examId,
                                //             categoryId: dataModel.categoryId,
                                //             columnId: _columnList[0].id!)
                                //         .then((res1) {
                                //       chapterList = res1;
                                //       if (mounted) {
                                //         setState(() {});
                                //       }
                                //     });
                                //   }else{
                                //     setState(() {});
                                //   }
                                //
                                // });
                              }),
                        )
                      ],
                    ),
                    // 显示视频
                    if (data == null)
                      SvgPicture.asset(
                        "images/home/empty.svg",
                        width: 200,
                      )
                    else
                      Card(
                        child: Container(
                          padding: EdgeInsets.all(16),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Icon(Icons.video_camera_front),
                              Expanded(child: Text(data["name"])),
                              TextButton(
                                  onPressed: () {
                                    Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            builder: (context) => FreeVideo(
                                                aliyunVideoId:
                                                    data["aliyunVideoId"],
                                                videoTitle: data["name"])));
                                  },
                                  child: Text("播放"))
                            ],
                          ),
                        ),
                      )
                    //   显示栏目及icon
                    //   Container(
                    //     height: 200,
                    //     margin: EdgeInsets.only(left: 16, right: 16),
                    //     child: CustomScrollView(
                    //       // scrollDirection: Axis.horizontal,
                    //       slivers: [
                    //         SliverGrid(
                    //           gridDelegate:
                    //               SliverGridDelegateWithMaxCrossAxisExtent(
                    //             mainAxisExtent: 0.25.sw,
                    //             maxCrossAxisExtent: 110,
                    //             // mainAxisSpacing: 10,
                    //             crossAxisSpacing: 10,
                    //           ),
                    //           delegate: SliverChildBuilderDelegate(
                    //             (context, index) {
                    //               return Container(
                    //                   // height: 50,
                    //                   child: GestureDetector(onTap: (){
                    //                     _selectedColumnIndex = index;
                    //                   //   重新获取chapterList
                    //                     chapterList =[];
                    //                     DataModel dataModel =
                    //                         dataUtilComm.dataModel;
                    //                     CourseDao.listByColumn(
                    //                         industryCode: dataModel.industryId,
                    //                         examId: dataModel.examId,
                    //                         categoryId: _categoryList![_selectedCategoryIndex].id!,
                    //                         columnId: _columnList[index].id!)
                    //                         .then((res1) {
                    //                       chapterList = res1;
                    //                       if (mounted) {
                    //                         setState(() {});
                    //                       }
                    //                     });
                    //                   },child: Column(
                    //                 mainAxisAlignment: MainAxisAlignment.center,
                    //                 crossAxisAlignment:
                    //                     CrossAxisAlignment.center,
                    //                 mainAxisSize: MainAxisSize.min,
                    //                 // spacing: 10,
                    //                 children: [
                    //                   SvgPicture.asset(
                    //                     tabIcons[index % 3],
                    //                     width: 48,
                    //                     height: 48,
                    //                   ),
                    //                   Container(
                    //                     child: Text(_columnList[index].name!),
                    //                     height: 20,
                    //                   )
                    //                 ],
                    //               )));
                    //             },
                    //             childCount: _columnList.length,
                    //           ),
                    //           // 设置水平滚动
                    //         ),
                    //       ],
                    //     ),
                    //   ),
                    // Container(
                    //   height: 1.sh-kToolbarHeight-ScreenUtil().statusBarHeight-200-60-10,
                    //   margin: EdgeInsets.only(left: 16, right: 16),
                    //   child: ListView.builder(
                    //     itemBuilder: _itemBuilder,
                    //     itemCount: chapterList.length,
                    //   ),
                    // )
                  ],
                )),
    );
  }
// Widget? _itemBuilder(BuildContext context, int index) {
//   if (index < chapterList.length) {
//     CourseChapterModel model = chapterList[index];
//     return RecommendExpandableWidget(
//       model: model,
//       columnId: _columnList[_selectedColumnIndex].id!,
//       free: model.free!,
//       // opening: index == 0 && _init,
//       opening: index == 0 ,
//       init: true,
//     );
//   } else {
//     return hiSpace(height: 80);
//   }
// }
}
